package eu.vranckaert.workRegistrationWidget.dao.impl;

import android.content.ContentValues;
import android.content.Context;
import android.location.Location;
import eu.vranckaert.workRegistrationWidget.dao.TimeRegDao;
import eu.vranckaert.workRegistrationWidget.dao.util.DaoHelperImpl;
import eu.vranckaert.workRegistrationWidget.dao.util.DatabaseVars;
import eu.vranckaert.workRegistrationWidget.domain.TimeRegistration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TimeRegDaoImpl extends DaoHelperImpl implements TimeRegDao {
    public TimeRegistration findLatestRegistration(Context context) {
        openForRead(context);

        cursor = this.db.rawQuery("select id, startdate, enddate, startLatitude, startLongtitude, endLatitude, endLongtitude from " + DatabaseVars.TABLE_TIMEREG + " order by startdate DESC LIMIT 1", null);

        TimeRegistration tr = new TimeRegistration();

        if (cursor.moveToNext()) {
            tr.setId(cursor.getInt(0));
            tr.setStartDate(new Date(cursor.getLong(1)));
            tr.setEndDate(new Date(cursor.getLong(2)));

            //Set starting longtitude and latitude
            if (cursor.getString(3) != null && !cursor.getString(3).equals("")
                    && cursor.getString(4) != null && !cursor.getString(4).equals("")) {
                tr.setStartLatitude(new Double(cursor.getString(3)));
                tr.setStartLongtitude(new Double(cursor.getString(4)));
            }

            //Set ending longtitude and latitude
            if (cursor.getString(5) != null && !cursor.getString(5).equals("")
                    && cursor.getString(6) != null && !cursor.getString(6).equals("")) {
                tr.setEndLatitude(new Double(cursor.getString(5)));
                tr.setEndLongtitude(new Double(cursor.getString(6)));
            }
        }

        if (tr.getEndDate() != null && tr.getEndDate().getTime() == 0L) {
            tr.setEndDate(null);
        }

        close();

        return tr;
    }

    public TimeRegistration saveOrUpdate(Context context, TimeRegistration timeRegistration) {
        openForWrite(context);

        ContentValues content = new ContentValues();
        content.put(DatabaseVars.TIMEREG_FIELDS_STARTDATE, Long.valueOf(timeRegistration.getStartDate().getTime()));
        if (timeRegistration.getEndDate() != null) {
            content.put(DatabaseVars.TIMEREG_FIELDS_ENDDATE, Long.valueOf(timeRegistration.getEndDate().getTime()));
        }

        if(timeRegistration.getStartLatitude() != null && timeRegistration.getStartLongtitude() != null) {
            content.put(DatabaseVars.TIMEREG_FIELDS_STARTLATITUDE, timeRegistration.getStartLatitude());
            content.put(DatabaseVars.TIMEREG_FIELDS_STARTLONGTITUDE, timeRegistration.getStartLongtitude());
        }

        if(timeRegistration.getEndLatitude() != null && timeRegistration.getEndLongtitude() != null) {
            content.put(DatabaseVars.TIMEREG_FIELDS_ENDLATITUDE, timeRegistration.getEndLatitude());
            content.put(DatabaseVars.TIMEREG_FIELDS_ENDLONGTITUDE, timeRegistration.getEndLongtitude());
        }

        if (timeRegistration.getId() == 0) {
            long id = this.db.insert(DatabaseVars.TABLE_TIMEREG, null, content);
            timeRegistration.setId(new Long(id).intValue());
        } else {
            this.db.update(DatabaseVars.TABLE_TIMEREG, content, DatabaseVars.TIMEREG_FIELDS_ID + "=?", new String[] { "" + timeRegistration.getId() });
        }

        close();

        return timeRegistration;
    }

    public List<TimeRegistration> findAll(Context context) {
        openForRead(context);

        cursor = this.db.query(DatabaseVars.TABLE_TIMEREG, new String[] {
                    DatabaseVars.TIMEREG_FIELDS_ID, DatabaseVars.TIMEREG_FIELDS_STARTDATE, DatabaseVars.TIMEREG_FIELDS_ENDDATE,
                    DatabaseVars.TIMEREG_FIELDS_STARTLATITUDE, DatabaseVars.TIMEREG_FIELDS_STARTLONGTITUDE,
                    DatabaseVars.TIMEREG_FIELDS_ENDLATITUDE, DatabaseVars.TIMEREG_FIELDS_ENDLONGTITUDE
                }, null, null, null, null, DatabaseVars.TIMEREG_FIELDS_STARTDATE + " " + DatabaseVars.DESC);

        List<TimeRegistration> registrations = new ArrayList<TimeRegistration>();
        while(cursor.moveToNext()) {
            TimeRegistration registration = new TimeRegistration();
            registration.setId(cursor.getInt(0));
            registration.setStartDate(new Date(cursor.getLong(1)));
            registration.setEndDate(new Date(cursor.getLong(2)));

            //Set starting longtitude and latitude
            if (cursor.getString(3) != null && !cursor.getString(3).equals("")
                    && cursor.getString(4) != null && !cursor.getString(4).equals("")) {
                registration.setStartLatitude(new Double(cursor.getString(3)));
                registration.setStartLongtitude(new Double(cursor.getString(4)));
            }

            //Set ending longtitude and latitude
            if (cursor.getString(5) != null && !cursor.getString(5).equals("")
                    && cursor.getString(6) != null && !cursor.getString(6).equals("")) {
                registration.setEndLatitude(new Double(cursor.getString(5)));
                registration.setEndLongtitude(new Double(cursor.getString(6)));
            }

            registrations.add(registration);
        }

        close();

        return registrations;
    }

    @Override
    public void removeAll(Context context) {
        openForWrite(context);

        int rows = this.db.delete(DatabaseVars.TABLE_TIMEREG, null, null);

        close();
    }

    @Override
    public void delete(Context context, TimeRegistration timeRegistration) {
        openForWrite(context);

        this.db.delete(DatabaseVars.TABLE_TIMEREG, DatabaseVars.TIMEREG_FIELDS_ID + " = " + timeRegistration.getId(), null);

        close();
    }

    @Override
    public void updateWithLocation(Context context, TimeRegistration timeRegistration, Location location, boolean startLocation) {
        TimeRegistration tr = findById(context, timeRegistration.getId());

        if (startLocation) {
            tr.setStartLongtitude(location.getLongitude());
            tr.setStartLatitude(location.getLatitude());
        } else {
            tr.setEndLatitude(location.getLatitude());
            tr.setEndLongtitude(location.getLongitude());
        }

        saveOrUpdate(context, tr);
    }

    private TimeRegistration findById(Context context, int id) {
        openForRead(context);

        cursor = this.db.rawQuery("select id, startdate, enddate, startLatitude, startLongtitude, endLatitude, endLongtitude from " + DatabaseVars.TABLE_TIMEREG + " where id=" + id, null);

        TimeRegistration tr = new TimeRegistration();

        if (cursor.moveToNext()) {
            tr.setId(cursor.getInt(0));
            tr.setStartDate(new Date(cursor.getLong(1)));
            tr.setEndDate(new Date(cursor.getLong(2)));

            //Set starting longtitude and latitude
            if (cursor.getString(3) != null && !cursor.getString(3).equals("")
                    && cursor.getString(4) != null && !cursor.getString(4).equals("")) {
                tr.setStartLatitude(new Double(cursor.getString(3)));
                tr.setStartLongtitude(new Double(cursor.getString(4)));
            }

            //Set ending longtitude and latitude
            if (cursor.getString(5) != null && !cursor.getString(5).equals("")
                    && cursor.getString(6) != null && !cursor.getString(6).equals("")) {
                tr.setEndLatitude(new Double(cursor.getString(5)));
                tr.setEndLongtitude(new Double(cursor.getString(6)));
            }
        }

        if (tr.getEndDate() != null && tr.getEndDate().getTime() == 0L) {
            tr.setEndDate(null);
        }

        close();

        return tr;
    }
}
